package com.gsn.engine.myplay;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.TweenCallback;

import com.gsn.engine.GsnException;
import com.gsn.engine.gdx.assets.GsnManagerTexture;
import com.gsn.engine.utility.UtilityGettor;

abstract public class GsnScreen{
	public GsnGame game;
	public List<GsnLayer> layers = new ArrayList<GsnLayer>();
	public float width;
	public float height;
	public boolean inited = false;
	public final List<GsnManagerTexture> assetNeedList = new ArrayList<GsnManagerTexture>();
	
	public GsnScreen(float width, float height) {
		this.width = width;
		this.height = height;
		setAssetNeed();
	}
	
	protected abstract void setAssetNeed();
	
	public abstract void onInit();
	
	abstract public GsnGame getGame();
	
	public void onShowScreen(){
		getGame().runInGLThread(new TweenCallback() {
			
			@Override
			public void onEvent(EventType eventType, BaseTween source) {
				try{
					finishInit();
				} catch (GsnException ex){
					ex.printStackTrace();
				}
				setDefaultInputListener();
				
			}
		}, 0);		
	}
	
	public void finishInit() throws GsnException{
		//UtilityGettor.inst().showProgressDialog("Loading", "Loading...");
		for (GsnManagerTexture tmp : assetNeedList)
			tmp.finishLoadingAndAssignContent();				
		if (!inited){
			onInit();
			inited = true;
		}
		//UtilityGettor.inst().hideProgressDialog();
	}
	
	public void onHideScreen(){
		
	}
	
	public void addLayer(GsnLayer layer){
		layer.screen = this;
		layers.add(layer);
		sortLayer();
		layer.screen = this;
		//layer.index = layers.get(layers.size() - 1).index + 1;
	}
	
	public void addAsset(GsnManagerTexture asset){
		if (!assetNeedList.contains(asset))
			assetNeedList.add(asset);
	}
	
	public void addLayer(GsnLayer layer, float index, boolean visible){
		layer.screen = this;
		layer.index = index;
		layer.visible = visible;
		layers.add(layer);
		sortLayer();
		layer.screen = this;
	}
	
	public void addLayerOnTop(GsnLayer layer, boolean visible){
		layer.screen = this;
		float max = -1000;
		for (GsnLayer tmp : layers)
			if (max < layer.index)
				max = layer.index;
		layer.index = max + 1;
		layer.visible = visible;
		layers.add(layer);
		sortLayer();
		layer.screen = this;
	}
	
	public void removeLayer(GsnLayer layer){
		layers.remove(layer);
	}
	
	public void sortLayer(){
		Collections.sort(layers);
	}	

	public int getRatioHeight(float ratio) {
		return (int) (ratio * height);
	}

	public int getRatioWidth(float ratio) {
		return (int) (ratio * width);
	}

	
	abstract public void setDefaultInputListener();

	
	public void act(float delta) {			
		for (GsnLayer tmp : layers)
			tmp.act(delta);
	}

	public void draw() {	
		for (GsnLayer tmp : layers)
			if (tmp.visible)
				tmp.draw();
	}
	
	public void setIndexLayer(GsnLayer layer, int index)
	{
		layer.index = index;
	}
	
	public void setVisibleLayer(GsnLayer layer, boolean visible)
	{
		layer.visible = visible;
	}
	
	public void setInputLayer(GsnLayer layer)
	{
		layer.setInputListener();
	}
	
	public void showAndSetListener(GsnLayer layer)
	{
		setInputLayer(layer);
		setVisibleLayer(layer, true);
	}

	public void hideChildsUnder(GsnLayer layer) {	
		for (GsnLayer tmp : layers){
			if (tmp.index > layer.index)
				tmp.visible = false;
		}		
	}
}